package com.zut.auth.application.controller;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.zut.auth.application.convert.AuthRoleDTOConverter;
import com.zut.auth.application.convert.AuthRolePermissionDTOConverter;
import com.zut.auth.application.dto.AuthRoleDTO;
import com.zut.auth.application.dto.AuthRolePermissionDTO;
import com.zut.auth.infra.entity.AuthRolePermission;
import com.zut.common.entity.Result;
import com.zut.domain.entity.AuthRoleBO;
import com.zut.domain.entity.AuthRolePermissionBO;
import com.zut.domain.sevice.AuthRoleDomainService;
import com.zut.domain.sevice.AuthRolePermissionDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/rolePermission/")
public class RolePermissionController {

    @Resource
    private AuthRolePermissionDomainService authRolePermissionDomainService;

    /**
     * 新增角色权限
     */
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody AuthRolePermissionDTO authRolePermissionDTO) {
        Result result =new Result();
        try {
            if (log.isInfoEnabled()) {
                log.info("RolePermissionController.add.dto:{}", JSON.toJSONString(authRolePermissionDTO));
            }
            //参数校验
            Preconditions.checkNotNull(authRolePermissionDTO.getRoleId(), "角色Id不能为空");

            AuthRolePermissionBO authRolePermissionBO = AuthRolePermissionDTOConverter.INSTANCE.convertDTOToBO(authRolePermissionDTO);
            return result.success(authRolePermissionDomainService.add(authRolePermissionBO));
        } catch (Exception e) {
            log.error("RoleController.register.error:{}", e.getMessage(), e);
            return result.fail("注册用户失败");
        }
    }


}
